package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.emtity.Category;
import com.itheima.reggie.emtity.Dish;
import com.itheima.reggie.emtity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*


 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    //新增菜品，同时插入菜品对应的口味数据，需要操作两张表：dish、dish_flavor
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavors(dishDto);
        return R.success("新增菜品成功");
    }

    //菜品信息分页查询
    //Get:http://localhost:8080/employee/page?page=1&pageSize=10
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //Dish
        Page<Dish> dishPage = new Page<>(page,pageSize);
        //DishDto
        Page<DishDto> dishDtoPage = new Page<>();

        //select * from  dish where name like “” order by desc update like 0，10
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Dish::getName,name);
        lqw.orderByDesc(Dish::getUpdateTime);
        //在dish表中分页查询
        dishService.page(dishPage,lqw);

        //拼接数据,对象拷贝
        //拷贝内容：page,pageSize,tortal------->无record
        BeanUtils.copyProperties(dishPage,dishDtoPage,"record");
        //================================================================================
        //处理  record
        //获取Dish中record，以获取菜品分类名称
        List<Dish> records = dishPage.getRecords();
        //将dto的数据装到一个集合
        List<DishDto> list = new ArrayList<>();
        for (Dish dish : records) {
            DishDto dishDto = new DishDto();
            //将Dish每条数据封装到dishdto
            BeanUtils.copyProperties(dish,dishDto);
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            dishDto.setCategoryName(category.getName());
            list.add(dishDto);
        }
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    //根据菜品ID查询菜品信息以及对应的口味信息
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIDWithFlavor(id);
        return R.success(dishDto);
    }


    //修改菜品以及口味
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavors(dishDto);
        return R.success("修改成功");
    }

    //根据菜品分类ID查询分类下所有的菜品信息
    //http://localhost:8080/dish/list?categoryId=1397844391040167938
    /*@GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        //select * from dish where category_id = ''
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,dish.getCategoryId());
        lqw.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(lqw);
        return R.success(list);
    }*/
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //select * from dish where category_id = ''
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        lqw.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(lqw);

        //查询口味表
        //将查询到的dishDto放到list2里
        List<DishDto> list2 = new ArrayList<>();
        for (Dish ds : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(ds,dishDto);
            LambdaQueryWrapper<DishFlavor> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(DishFlavor::getDishId,ds.getId());
            List<DishFlavor> list3 = dishFlavorService.list(lqw2);
            dishDto.setFlavors(list3);
            list2.add(dishDto);
        }
        return R.success(list2);
        //return R.success(list);
    }
	
	 /*
    *若删除的菜品试起售状态，抛出异常，
    * 若是停售状态，则删除
    */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteWithFlavor(ids);
        return R.success("删除成功");
    }
}
